package com.fastmobile.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fastmobile.dto.Xml1Dto;
import com.fastmobile.dto.Xml2Dto;
import com.fastmobile.util.Constant;
import com.fastmobile.util.Util;
import com.fastmobile.xml.XmlProcess;

/**
 * Servlet implementation class Service
 */
public class Service extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static ResourceBundle resourceBundle;
    /**
     * Default constructor. 
     */
    public Service() {
        resourceBundle = ResourceBundle.getBundle("config");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XmlProcess xmlProcess = new XmlProcess();
		Xml2Dto xml2Dto = xmlProcess.readXml(resourceBundle.getString(Constant.XML2));
		response.setContentType("text/html");
	    PrintWriter out = response.getWriter();
	    if (null != xml2Dto) {
	    	out.println("Khoang cach: " + xml2Dto.getDistance() + "<br>");
	    	out.println("Vi do: " + xml2Dto.getLatitute() + "<br>");
	    	out.println("Kinh do: " + xml2Dto.getLongtitute() + "<br>");
	    	out.println("Nhien lieu: " + xml2Dto.getFuel() + "<br>");
	    	out.println("Phan hoi tim xe: " + xml2Dto.getFindFeedBack() + "<br>");
	    	out.println("Phan hoi unlock: " + xml2Dto.getUnlockFeedBack() + "<br>");
	    	out.println("Phan hoi lock: " + xml2Dto.getLockFeedBack() + "<br>");
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String alert = request.getParameter("alert");
		String lock = request.getParameter("lock");
		String unlock = request.getParameter("unlock");
		String status = request.getParameter("status");
		
		Xml1Dto xml1Dto = new Xml1Dto();
		xml1Dto.setAlert(alert);
		xml1Dto.setLock(lock);
		xml1Dto.setUnlock(unlock);
		xml1Dto.setStatus(status);
		
		XmlProcess xmlProcess = new XmlProcess();
		xmlProcess.modifyXml(resourceBundle.getString(Constant.XML1), xml1Dto);
	}
}

